package com.coisshi.base.module.captcha.controller;

import cn.dev33.satoken.util.SaResult;
import com.coisshi.base.common.annotation.PreventDuplicateSubmit;
import com.coisshi.base.module.captcha.utils.CaptchaTool;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码接口
 */
@RestController
@RequestMapping("/api/captcha")
@RequiredArgsConstructor
public class CaptchaController {

    private final CaptchaTool captchaTool;

    /**
     * 获取验证码图片（返回 codeId + base64）
     */
    @PreventDuplicateSubmit(intervalTime = 6)
    @GetMapping("/generate")
    public SaResult generate() {
        CaptchaTool.CaptchaInfo captcha = captchaTool.generateCaptcha();
        return SaResult.ok()
                .set("codeId", captcha.getCodeId())
                .set("base64", "data:image/png;base64," + captcha.getBase64());
    }

    /**
     * 校验验证码
     */
    @PostMapping("/verify")
    public SaResult verify(@RequestParam String codeId, @RequestParam String code) {
        boolean pass = captchaTool.validate(codeId, code);
        return pass ? SaResult.ok("验证码通过") : SaResult.error("验证码错误");
    }
} 